package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.pharmacy.bean.GroupListBean;
import com.zhiyizhen.handheld.component.pharmacy.view.RegimenShuyeUnitLayout;
import com.zhiyizhen.handheld.databinding.ItemRegimenShuyeGroupBinding;

public class RegimenShuyeGroupAdapter extends BaseAdapter<GroupListBean, RegimenShuyeGroupAdapter.ShuyeGroupViewHolder> {
    private OnCheckedChangeListener onCheckedChangeListener;

    public RegimenShuyeGroupAdapter(Context context) {
        super(context);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @NonNull
    @Override
    public ShuyeGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ShuyeGroupViewHolder(this, ItemRegimenShuyeGroupBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ShuyeGroupViewHolder extends BaseViewHolder<GroupListBean> {

        private final ItemRegimenShuyeGroupBinding binding;

        public ShuyeGroupViewHolder(BaseAdapter<GroupListBean, ShuyeGroupViewHolder> adapter, ItemRegimenShuyeGroupBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(GroupListBean data) {
            binding.groupNameTv.setText(data.getGroupName());

            if (TextUtils.equals(data.getDispenseStatus(), "3")) {
                binding.checkbox.setEnabled(false);
            } else {
                binding.checkbox.setEnabled(true);
            }

            binding.checkbox.setChecked(TextUtils.equals(data.getDispenseStatus(), "1"));
            binding.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (onCheckedChangeListener != null) {
                    onCheckedChangeListener.onCheckedChanged(data, isChecked);
                }
                data.setDispenseStatus(isChecked ? "1" : "0");
            });

            binding.usageCodeTextTv.setText(data.getUsageCodeText());
            binding.frequencyCodeTextTv.setText(data.getFrequencyCodeText());
            binding.totalDayTv.setText(data.getCycle() + data.getCycleUnitText());

            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            if (data.getDetailList() != null && !data.getDetailList().isEmpty()) {
                RegimenShuyeUnitLayout shuyeUnitLayout;
                for (int i = 0; i < data.getDetailList().size(); i++) {
                    shuyeUnitLayout = new RegimenShuyeUnitLayout(itemView.getContext(), i + 1, data.getDetailList().get(i));
                    binding.shuyeInfoLayout.addView(shuyeUnitLayout);
                }
            }
        }
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(GroupListBean data, boolean isChecked);
    }
}